var app = getApp()
var modal = require('../../utils/modal');
var request = require('../../utils/request');
var utils = require('../../utils/utils');
var comment = require('../widget/comment/comment');
var goTop = require('../widget/goTop/goTop');

var pageSetting = {
    callback:function(componentName){
        var that = this;
        switch(componentName){
            case 'pullUpLoad':
                pullUpLoad();
                break;
            default:
                break;
        }

        //上拉加载
        function pullUpLoad(){
            that.setData({
                upLoadFlag:0
            });
            modal.loading();
            var data = {
                'yp:api:article:list':{
                    'section_id':that['data']['id'],
                    'page':that['data']['upLoadPage'],
                    'pageSize':10
                }
            };
            request.post({
                path: '/wxapp_api/api'
            },data)
            .then(function (res){
                if(res['yp:api:article:list']['ret'] != 0){
                    modal.hide();
                    // modal.toast(res['yp:api:article:list']['msg']);
                    wx.navigateTo({
                        url: "../error/error"
                    })
                    return false;
                }
          
                var homeDataList = res['yp:api:article:list']['data'];
                
                if(homeDataList['list'].length < 10){
                    that.setData({
                        upLoadFlag:0
                    });
                }else{
                    that.setData({
                        upLoadFlag:1
                    });
                }
                
                that['data']['homeDataList']['list'] = that['data']['homeDataList']['list'].concat(homeDataList['list']);

                that.setData({
                    homeDataList:that['data']['homeDataList']
                });
                modal.hide();
            });
        }
    },
    data: {
        shareContent:{}
    },
    onLoad: function(options) {
        modal.loading();
        var that = this,
        data = {
          "yp:api:article:list" : {
              'section_id':options['id'],
              'page':1,
              'page_size':10
          }
        }
        that.setData({
            id:options['id']
        });
        request.post({
            path: '/wxapp_api/api'
        },data)
        .then(function (res){
            if(res['yp:api:article:list']['ret'] != 0){
                // modal.toast(res['yp:api:article:list']['msg']);
                wx.navigateTo({
                    url: "../error/error"
                })
                return false;
            }
          
            var homeDataList = res['yp:api:article:list']['data'];

            if(homeDataList['list'].length < 10){
                that.setData({
                    upLoadFlag:0
                });
            }
            wx.setNavigationBarTitle({
                title: homeDataList['section_info']['section_name']
            })

            var shareContent = {
                'share_title' : homeDataList['share']['share_title'],
                'share_desc': homeDataList['share']['share_desc'],
                'share_url': 'pages/article-list/index?id='+options['id']
            }

            that.setData({
                homeDataList:homeDataList,
                shareContent:shareContent
            });
            modal.hide();
        });
    },
    onShareAppMessage:function(){
        var that = this;
        return {
            title: that['data']['shareContent']['share_title'],
            desc:that['data']['shareContent']['share_desc'],
            path: that['data']['shareContent']['shar_url']
        }
    }
};

utils.assign(pageSetting,comment,goTop);
Page(pageSetting);